﻿Public Class xtf_ABMlistaprecio

    Dim ulg_row As DataRow
    Dim obj_ListaPrecio As cls_listaprecio_ = Nothing
    Dim dtt_listaprecioarticulo_ As DataTable

    Public Sub New()
        InitializeComponent()
        AgregarValidaciones()
        'cbo_Estado.SelectedIndex = 0
        dtp_Fecha.DateTime = Now

        'Prepara grid y datasource proveedorarticulo----------------------------------------------------
        dtt_listaprecioarticulo_ = New DataTable
        dtt_listaprecioarticulo_.Columns.Add("idarticulo", GetType(Integer))
        dtt_listaprecioarticulo_.Columns.Add("Nombre", GetType(String))
        dtt_listaprecioarticulo_.Columns.Add("Descripcion", GetType(String))
        dtt_listaprecioarticulo_.Columns.Add("Rubro", GetType(String))
        dtt_listaprecioarticulo_.Columns.Add("Costo", GetType(Decimal))
        dtt_listaprecioarticulo_.Columns.Add("Valor", GetType(Decimal))
        dtt_listaprecioarticulo_.Columns.Add("Unidad", GetType(String))
        dtt_listaprecioarticulo_.Columns.Add("Moneda", GetType(Object))
        dtt_listaprecioarticulo_.Columns.Add("Tipo", GetType(Integer))
        dtt_listaprecioarticulo_.Columns.Add("Formula", GetType(String))
        dtt_listaprecioarticulo_.Columns.Add("objeto", GetType(cls_listaprecioarticulo_))

        dtt_listaprecioarticulo_.Columns.Add("ListaCosto", GetType(Decimal))
        dtt_listaprecioarticulo_.Columns.Add("ListaCostoVariacion", GetType(Decimal))
        dtt_listaprecioarticulo_.Columns.Add("ListaValor", GetType(Decimal))
        dtt_listaprecioarticulo_.Columns.Add("ListaValorVariacion", GetType(Decimal))

        gdc_listaprecioarticulo_.DataSource = dtt_listaprecioarticulo_
        gdc_listaprecioarticulo_.ForceInitialize()

        gdv_listaprecioarticulo_.PopulateColumns()
        gdv_listaprecioarticulo_.Columns("Valor").Caption = "Valor venta"
        gdv_listaprecioarticulo_.Columns("objeto").Visible = False
        gdv_listaprecioarticulo_.Columns("idarticulo").Visible = False

        gdv_listaprecioarticulo_.Columns("Nombre").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("Descripcion").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("Rubro").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("Unidad").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("Costo").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("Tipo").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("Formula").OptionsColumn.AllowEdit = False

        gdv_listaprecioarticulo_.Columns("ListaCosto").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("ListaCostoVariacion").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("ListaValor").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("ListaValorVariacion").OptionsColumn.AllowEdit = False
        gdv_listaprecioarticulo_.Columns("ListaCosto").Visible = False
        gdv_listaprecioarticulo_.Columns("ListaValor").Visible = False
        gdv_listaprecioarticulo_.Columns("ListaCostoVariacion").Visible = False
        gdv_listaprecioarticulo_.Columns("ListaValorVariacion").Visible = False

        gdv_listaprecioarticulo_.Columns("Valor").ColumnEdit = New DevExpress.XtraEditors.Repository.RepositoryItemTextEdit
        CType(gdv_listaprecioarticulo_.Columns("Valor").ColumnEdit, DevExpress.XtraEditors.Repository.RepositoryItemTextEdit).Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
        CType(gdv_listaprecioarticulo_.Columns("Valor").ColumnEdit, DevExpress.XtraEditors.Repository.RepositoryItemTextEdit).Mask.EditMask = "n" & int_cantdecimales
        CType(gdv_listaprecioarticulo_.Columns("Valor").ColumnEdit, DevExpress.XtraEditors.Repository.RepositoryItemTextEdit).Mask.UseMaskAsDisplayFormat = True

        gdv_listaprecioarticulo_.Columns("Moneda").ColumnEdit = New DevExpress.XtraEditors.Repository.RepositoryItemComboBox
        CType(gdv_listaprecioarticulo_.Columns("Moneda").ColumnEdit, DevExpress.XtraEditors.Repository.RepositoryItemComboBox).TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor
        Dim moneda_ As New cls_moneda_
        If moneda_.CargarTodos Then
            For Each moneda As cls_moneda_ In moneda_.Items
                CType(gdv_listaprecioarticulo_.Columns("Moneda").ColumnEdit, DevExpress.XtraEditors.Repository.RepositoryItemComboBox).Items.Add(moneda)
            Next
        End If
        '-----------------------------------------------------------------------------------------------
        Cargarmoneda()
        Cargararticulorubro_()
        cbo_articulorubro.Enabled = False
    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            obj_ListaPrecio = New cls_listaprecio_
            btn_Registro.Enabled = False
            Dim tmp_moneda As New cls_moneda_
            tmp_moneda.MonedaLocal()

            dtt_listaprecioarticulo_.Rows.Clear()
            Dim articulo_ As New cls_articulo_
            If articulo_.CargarTodosEnventa Then
                For Each articulo As cls_articulo_ In articulo_.Items
                    Dim row As DataRow = dtt_listaprecioarticulo_.NewRow
                    row("idarticulo") = articulo.idarticulo
                    row("Nombre") = articulo.nombreventa
                    row("Descripcion") = articulo.descripcion
                    row("Rubro") = articulo.articulorubro.ToString
                    row("Costo") = articulo.costo()
                    row("Valor") = 0
                    row("Unidad") = articulo.unidad.ToString
                    row("Moneda") = tmp_moneda
                    row("objeto") = Nothing
                    row("Formula") = 0
                    row("Tipo") = 0
                    row("ListaCosto") = 0
                    row("ListaCostoVariacion") = 0
                    row("ListaValor") = 0
                    row("ListaValorVariacion") = 0
                    dtt_listaprecioarticulo_.Rows.Add(row)
                Next
            End If

            Me.Show()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef ListaPrecio As cls_listaprecio_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_ListaPrecio) Then
            obj_ListaPrecio = New cls_ListaPrecio_
        End If
        obj_ListaPrecio = ListaPrecio
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_ListaPrecio.ToString
        txt_ID.Text = obj_ListaPrecio.idListaPrecio
        txt_Nombre.Text = obj_ListaPrecio.nombre
        txt_descripcion.Text = obj_ListaPrecio.descripcion
        dtp_Fecha.DateTime = obj_ListaPrecio.fecha
        txt_descripcion.Text = obj_ListaPrecio.descripcion
        opt_estado.SelectedIndex = obj_ListaPrecio.estado


        'CARGO TODA LA LISTA DE ARTICULOS, Y SUS LISTAPRECIOARTICULO SI TIENE----------
        dtt_listaprecioarticulo_.Rows.Clear()
        Dim tmp_moneda As New cls_moneda_
        tmp_moneda.MonedaLocal()
        Dim articulo_ As New cls_articulo_
        If articulo_.CargarTodosEnventa Then
            For Each articulo As cls_articulo_ In articulo_.Items
                Dim listaprecioarticulo As New cls_listaprecioarticulo_
                Dim dtt_row As DataRow = dtt_listaprecioarticulo_.NewRow
                dtt_row("idarticulo") = articulo.idarticulo
                dtt_row("Nombre") = articulo.nombreventa
                dtt_row("Descripcion") = articulo.descripcion
                dtt_row("Rubro") = articulo.articulorubro.ToString
                dtt_row("Unidad") = articulo.unidad.ToString
                If listaprecioarticulo.BuscarXidarticuloXidlistaprecio(articulo.idarticulo, obj_ListaPrecio.idlistaprecio) Then
                    dtt_row("Valor") = listaprecioarticulo.valor
                    dtt_row("objeto") = listaprecioarticulo
                    dtt_row("Moneda") = listaprecioarticulo.moneda
                    dtt_row("Costo") = listaprecioarticulo.costo
                    dtt_row("Tipo") = listaprecioarticulo.tipo
                    dtt_row("Formula") = listaprecioarticulo.formula
                Else
                    dtt_row("Formula") = 0
                    dtt_row("Tipo") = 0
                    dtt_row("Costo") = articulo.costo()
                    dtt_row("Valor") = 0
                    dtt_row("objeto") = Nothing
                    dtt_row("Moneda") = tmp_moneda
                End If
                dtt_row("ListaCosto") = 0
                dtt_row("ListaCostoVariacion") = 0
                dtt_row("ListaValor") = 0
                dtt_row("ListaValorVariacion") = 0
                dtt_listaprecioarticulo_.Rows.Add(dtt_row)
            Next
        End If
        '------------------------------------------------------------------------------

        If Not IsNothing(txt_listaprecio.Tag) Then
            compararLista(txt_listaprecio.Tag, False)
        End If
        btn_Registro.Enabled = True
        cbo_tipo.SelectedIndex = 0
        obj_ListaPrecio.listaprecioarticulo_ = Nothing
        obj_ListaPrecio.registro = Nothing
        Me.Show()

    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If
        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_ListaPrecio.nombre = txt_Nombre.Text
            obj_ListaPrecio.descripcion = txt_descripcion.Text
            obj_ListaPrecio.fecha = dtp_Fecha.DateTime
            obj_ListaPrecio.estado = opt_estado.SelectedIndex

            obj_ListaPrecio.listaprecioarticulo_ = New cls_listaprecioarticulo_
            obj_ListaPrecio.listaprecioarticulo_.Items = New Collections.Generic.List(Of cls_listaprecioarticulo_)
            For Each row As DataRow In dtt_listaprecioarticulo_.Rows
                Dim listaprecioarticulo As cls_listaprecioarticulo_
                If Not IsDBNull(row("objeto")) Then
                    listaprecioarticulo = row("objeto")
                Else
                    listaprecioarticulo = New cls_listaprecioarticulo_
                End If
                listaprecioarticulo.idarticulo = row("idarticulo")
                listaprecioarticulo.valor = row("Valor")
                listaprecioarticulo.idmoneda = CType(row("Moneda"), cls_moneda_).idmoneda
                listaprecioarticulo.costo = row("Costo")
                listaprecioarticulo.tipo = row("Tipo")
                listaprecioarticulo.formula = row("Formula")
                obj_ListaPrecio.listaprecioarticulo_.Items.Add(listaprecioarticulo)
            Next

            If txt_ID.Text = "" Then
                obj_ListaPrecio.Nuevo(True)
                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_LISTADOlistaprecio_).AgregarItem(obj_ListaPrecio)
                End If
                Modificar(obj_ListaPrecio, frm_listado, ulg_row)
            Else
                obj_ListaPrecio.Modificar()
                If frm_listado.Visible Then
                    CType(frm_listado, xtf_LISTADOlistaprecio_).ModificarItem(obj_ListaPrecio, ulg_row)
                End If
                Modificar(obj_ListaPrecio, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Registro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Registro.Click
        If Not IsNothing(obj_ListaPrecio.registro) Then
            Dim ABMRegistro As New xtf_ABMRegistro(obj_ListaPrecio.registro)
            If ABMRegistro.ShowDialog() = Windows.Forms.DialogResult.OK Then
                flg_Modificado = True
            End If
        End If
    End Sub


    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

    Private Sub cbo_tipo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbo_tipo.KeyPress
        If e.KeyChar = Chr(13) Then
            txt_formula.Focus()
        End If
    End Sub

    Private Sub cbo_tipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_tipo.SelectedIndexChanged
        If cbo_tipo.SelectedIndex = 2 Then
            txt_formula.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None
            txt_formula.Properties.Mask.EditMask = ""
            txt_formula.Properties.Buttons(0).Visible = True
            txt_formula.Properties.ReadOnly = True
        Else
            txt_formula.Properties.Buttons(0).Visible = False
            txt_formula.Properties.ReadOnly = False
            txt_formula.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
            txt_formula.Properties.Mask.EditMask = "n" & int_cantdecimales
        End If
        txt_formula_EditValueChanged(Nothing, Nothing)
    End Sub

    Private Sub txt_formula_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_formula.EditValueChanged
        Select Case cbo_tipo.SelectedIndex
            Case cls_alteracion_.enu_tipo.Valor
                'txt_Valor.Text = txt_formula.Text
            Case cls_alteracion_.enu_tipo.Porcentaje
                'txt_Valor.Text = Math.Round((txt_formula.Text * subTotal) / 100, int_cantdecimales)
        End Select
    End Sub

    Private Sub txt_formula_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_formula.KeyPress
        If e.KeyChar = Chr(13) Then
            'tsb_documentoagrupacionAgregar.Focus()
        End If
    End Sub

    Private Sub txt_alteracion_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txt_alteracion.ButtonClick
        Dim buscar As xtf_buscardor = Nothing
        Dim Objeto_ As New cls_alteracion_
        Objeto_.CargarTodos()

        buscar = New xtf_buscardor("Alteraciones", Objeto_, {"nombre|Nombre", "descripcion|Descripcion"})

        If buscar.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim alteracion As cls_alteracion_ = buscar.obj_resultado
            txt_Nombre.Tag = alteracion
            txt_Nombre.Text = alteracion.nombre
            cbo_tipo.SelectedIndex = alteracion.tipo
            txt_formula.Text = alteracion.formula

            txt_Nombre.Properties.ReadOnly = True
        End If
    End Sub

    Private Sub txt_nombre_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_Nombre.KeyDown
        If e.KeyCode = Keys.F3 Then
            txt_alteracion.PerformClick(txt_alteracion.Properties.Buttons(0))
            Exit Sub
        End If
        If e.KeyCode <> 13 Then
            If Not IsNothing(txt_nombre.Tag) Then
                txt_nombre.Tag = Nothing
            End If
            txt_nombre.Properties.ReadOnly = False
        End If

    End Sub

    Private Sub txt_nombre_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_Nombre.KeyPress
        If e.KeyChar = Chr(13) Then
            cbo_tipo.Focus()
        Else
            If Not IsNothing(txt_nombre.Tag) Then
                txt_nombre.Tag = Nothing
            End If
            txt_nombre.Properties.ReadOnly = False
        End If
    End Sub

    Private Sub txt_formula_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txt_formula.ButtonClick
        Dim ABMFormula As New xtf_ABMFormula(txt_formula.Text)
        If ABMFormula.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txt_formula.Text = ABMFormula.txt_formula.Text
        End If
    End Sub

    Private Sub opt_aplicarA_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles opt_aplicarA.SelectedIndexChanged
        If opt_aplicarA.SelectedIndex = 3 Then
            Cargararticulorubro_()
            cbo_articulorubro.Enabled = True
        Else
            cbo_articulorubro.Enabled = False
        End If
    End Sub

    Private Sub Cargararticulorubro_()
        Dim tmp_obj As Object = Nothing
        If Not IsNothing(cbo_articulorubro.SelectedItem) Then
            tmp_obj = cbo_articulorubro.SelectedItem
        End If

        cbo_articulorubro.Properties.Items.Clear()
        Dim articulorubro_ As New cls_articulorubro_
        If articulorubro_.CargarTodos Then
            For Each articulorubro As cls_articulorubro_ In articulorubro_.Items
                cbo_articulorubro.Properties.Items.Add(articulorubro)
            Next
            cbo_articulorubro.SelectedIndex = 0
            cbo_articulorubro.Enabled = True

            If Not IsNothing(tmp_obj) Then
                For Each obj As cls_articulorubro_ In cbo_articulorubro.Properties.Items
                    If obj.idarticulorubro = tmp_obj.idarticulorubro Then
                        cbo_articulorubro.SelectedItem = obj
                        Exit For
                    End If
                Next
            End If
        Else
            cbo_articulorubro.Enabled = False
            cbo_articulorubro.SelectedItem = Nothing
        End If
    End Sub

    Private Sub Cargarmoneda()
        Dim tmp_obj As Object = Nothing
        If Not IsNothing(cbo_moneda.SelectedItem) Then
            tmp_obj = cbo_moneda.SelectedItem
        End If
        cbo_moneda.Properties.Items.Clear()

        Dim moneda_ As New cls_moneda_
        If moneda_.CargarTodos Then
            For Each moneda As cls_moneda_ In moneda_.Items
                cbo_moneda.Properties.Items.Add(moneda)
            Next
            cbo_moneda.SelectedIndex = 0
            cbo_moneda.Enabled = True

            If Not IsNothing(tmp_obj) Then
                For Each obj As cls_moneda_ In cbo_moneda.Properties.Items
                    If obj.idmoneda = tmp_obj.idmoneda Then
                        cbo_moneda.SelectedItem = obj
                        Exit For
                    End If
                Next
            End If
        Else
            cbo_moneda.Enabled = True
            cbo_moneda.SelectedItem = Nothing
        End If
    End Sub

    Private Sub btn_aplicar_Click(sender As Object, e As System.EventArgs) Handles btn_aplicar.Click
        Select Case opt_aplicarA.SelectedIndex
            Case 0 'Todos
                For Each row As DataRow In dtt_listaprecioarticulo_.Rows
                    If chk_Alteracion.Checked Then
                        row("Tipo") = cbo_tipo.SelectedIndex
                        row("Formula") = txt_formula.Text
                        ResolverValor(row)
                    End If
                    If chk_moneda.Checked Then
                        row("Moneda") = CType(cbo_moneda.SelectedItem, cls_moneda_)
                    End If
                Next
            Case 1 'Seleccion
                For Each rowhandles As Integer In gdv_listaprecioarticulo_.GetSelectedRows
                    Dim row As DataRow = gdv_listaprecioarticulo_.GetDataRow(rowhandles)
                    If chk_Alteracion.Checked Then
                        row("Tipo") = cbo_tipo.SelectedIndex
                        row("Formula") = txt_formula.Text
                        ResolverValor(row)
                    End If
                    If chk_moneda.Checked Then
                        row("Moneda") = CType(cbo_moneda.SelectedItem, cls_moneda_)
                    End If
                Next
            Case 2 'Visibles
                Dim i As Integer = gdv_listaprecioarticulo_.GetVisibleRowHandle(0)
                Dim row As DataRow = gdv_listaprecioarticulo_.GetDataRow(i)
                While True
                    If IsNothing(row) Then
                        Exit While
                    End If
                    If chk_Alteracion.Checked Then
                        row("Tipo") = cbo_tipo.SelectedIndex
                        row("Formula") = txt_formula.Text
                        ResolverValor(row)
                    End If
                    If chk_moneda.Checked Then
                        row("Moneda") = CType(cbo_moneda.SelectedItem, cls_moneda_)
                    End If
                    i = gdv_listaprecioarticulo_.GetNextVisibleRow(i)
                    row = gdv_listaprecioarticulo_.GetDataRow(i)
                End While
            Case 3 'Rubros
                For Each row As DataRow In dtt_listaprecioarticulo_.Rows
                    If row("Rubro") = cbo_articulorubro.Text Then
                        If chk_Alteracion.Checked Then
                            row("Tipo") = cbo_tipo.SelectedIndex
                            row("Formula") = txt_formula.Text
                            ResolverValor(row)
                        End If
                        If chk_moneda.Checked Then
                            row("Moneda") = CType(cbo_moneda.SelectedItem, cls_moneda_)
                        End If
                    End If
                Next
        End Select
    End Sub

    Private Sub ResolverValor(ByRef prm_row As DataRow)
        Select Case prm_row("Tipo")
            Case cls_alteracion_.enu_tipo.Valor
                prm_row("Valor") = Math.Round(prm_row("Costo") + prm_row("Formula"), int_cantdecimales)
            Case cls_alteracion_.enu_tipo.Porcentaje
                prm_row("Valor") = Math.Round(prm_row("Costo") + (CDec(prm_row("Formula")) * prm_row("Costo") / 100), int_cantdecimales)
            Case cls_alteracion_.enu_tipo.Formula
                Dim tmp_listaprecioarticulo As New cls_listaprecioarticulo_
                tmp_listaprecioarticulo.costo = prm_row("Costo")
                tmp_listaprecioarticulo.valor = prm_row("Valor")
                tmp_listaprecioarticulo.formula = prm_row("Formula")
                prm_row("Valor") = tmp_listaprecioarticulo.Resolver
        End Select
        prm_row("ListaCostoVariacion") = prm_row("Costo") - prm_row("ListaCosto")
        prm_row("ListaValorVariacion") = prm_row("Valor") - prm_row("ListaValor")
    End Sub

    Private Sub chk_colComparativas_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chk_colComparativas.CheckedChanged
        gdv_listaprecioarticulo_.Columns("ListaCosto").Visible = chk_colComparativas.Checked
        gdv_listaprecioarticulo_.Columns("ListaValor").Visible = chk_colComparativas.Checked
        gdv_listaprecioarticulo_.Columns("ListaCostoVariacion").Visible = chk_colComparativas.Checked
        gdv_listaprecioarticulo_.Columns("ListaValorVariacion").Visible = chk_colComparativas.Checked
    End Sub

    Private Sub txt_listaprecio_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txt_listaprecio.ButtonClick
        Dim buscar As xtf_buscardor = Nothing
        Dim Objeto_ As New cls_listaprecio_
        Objeto_.CargarTodos()

        buscar = New xtf_buscardor("Listas de Precio", Objeto_, {"idlistaprecio|Numero", "nombre|Nombre"})

        If buscar.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim listaprecio As cls_listaprecio_ = buscar.obj_resultado
            txt_listaprecio.Tag = listaprecio
            txt_listaprecio.Text = listaprecio.ToString
            Dim formulas As Boolean = ErrManagerShow("003001", Me)
            compararLista(listaprecio, formulas)
        End If
    End Sub

    Private Sub compararLista(ByRef prm_listaprecio As cls_listaprecio_, ByRef prm_formulas As Boolean)
        For Each row As DataRow In dtt_listaprecioarticulo_.Rows
            Dim listaprecioarticulo As New cls_listaprecioarticulo_
            Dim dtt_row As DataRow = row
            If listaprecioarticulo.BuscarXidarticuloXidlistaprecio(dtt_row("idarticulo"), prm_listaprecio.idlistaprecio) Then
                If prm_formulas Then
                    row("Tipo") = listaprecioarticulo.tipo
                    row("Formula") = listaprecioarticulo.formula
                    ResolverValor(dtt_row)
                End If
                dtt_row("ListaCosto") = listaprecioarticulo.costo
                dtt_row("ListaCostoVariacion") = dtt_row("Costo") - dtt_row("ListaCosto")
                dtt_row("ListaValor") = listaprecioarticulo.valor
                dtt_row("ListaValorVariacion") = dtt_row("Valor") - dtt_row("ListaValor")
            Else
                dtt_row("ListaCosto") = 0
                dtt_row("ListaValor") = 0
                dtt_row("ListaCostoVariacion") = dtt_row("Costo") - dtt_row("ListaCosto")
                dtt_row("ListaValorVariacion") = dtt_row("Valor")-dtt_row("ListaValor") 
            End If
        Next
    End Sub

    Private Sub gdv_listaprecioarticulo__CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles gdv_listaprecioarticulo_.CellValueChanged
        Select Case e.Column.FieldName
            Case "Valor"
                Dim row As DataRow = gdv_listaprecioarticulo_.GetDataRow(e.RowHandle)
                If IsDBNull(row("Valor")) Then
                    row("Valor") = 0
                End If
                row("Tipo") = 0
                row("Formula") = row("Valor") - row("Costo")
                ResolverValor(gdv_listaprecioarticulo_.GetDataRow(e.RowHandle))
                'Case "Moneda"
                '    Dim row As DataRow = gdv_listaprecioarticulo_.GetDataRow(e.RowHandle)
                '    row("Costo") = CType(e.Value, cls_moneda_).cotizar(row("Costo"), row("Moneda"))
            Case Else
                ResolverValor(gdv_listaprecioarticulo_.GetDataRow(e.RowHandle))
        End Select
    End Sub

    Private Sub gdv_listaprecioarticulo__CellValueChanging(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles gdv_listaprecioarticulo_.CellValueChanging
        Select Case e.Column.FieldName
          
        End Select
    End Sub

    Private Sub gdv_listaprecioarticulo__RowCellStyle(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs) Handles gdv_listaprecioarticulo_.RowCellStyle
        If e.Column.Name.Contains("Variacion") Then
            If Not IsDBNull(e.CellValue) Then
                If e.CellValue < 0 Then
                    e.Appearance.ForeColor = Color.Red
                    e.Appearance.Font = New Font(e.Appearance.Font, FontStyle.Bold)
                ElseIf e.CellValue = 0 Then
                    e.Appearance.ForeColor = Color.Black
                    e.Appearance.Font = New Font(e.Appearance.Font, FontStyle.Regular)
                Else
                    e.Appearance.ForeColor = Color.Green
                    e.Appearance.Font = New Font(e.Appearance.Font, FontStyle.Bold)
                End If
            End If
            
        End If
    End Sub

    Public Overrides Sub bbi_imprimir_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If txt_ID.Text <> "" Then
            Dim obj_command As New SqlClient.SqlCommand()
            Dim obj_Conexion As New cls_Conexion
            obj_Conexion.Conectar()
            With obj_command
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "listaprecioarticulo__reporte"
                .Parameters.Add("@idlistaprecio", SqlDbType.Int).Value = txt_ID.Text
            End With
            Dim obj_Adapter As New SqlClient.SqlDataAdapter(obj_command)
            Dim dts_Movimientos As New dts_listaprecio
            obj_Adapter.Fill(dts_Movimientos.Tables(0))

            Dim report As New xtr_REPORTElistaprecio
            report.DataSource = dts_Movimientos

            report.lbl_nombre.Text = obj_ListaPrecio.nombre
            report.lbl_numero.Text = "Lista Nº: " & obj_ListaPrecio.idlistaprecio

            obj_Conexion.Desconectar()
            'report.lblFecha.Text = dteFecha.ToShortDateString
            'report.lblFecha2.Text = dteFecha.ToShortDateString
            report.ShowPreview()
        End If
    End Sub
End Class